#include <stdlib.h>
#include <unistd.h>
#include "queue.h"

struct s_queue
{
    void        *elem;
    queue       next;
};

queue q_push (void *x, queue s)
{
    queue       tmp = NULL;

    tmp = malloc (sizeof(struct s_queue));
    tmp->elem = x;
    tmp->next = NULL;

    if (s)
    {
        tmp->next = s->next;
        s->next = tmp;
    }
    else
    {
        tmp->next = tmp;
        s = tmp;
    }
    s = tmp;
    return s;
}

void *take (queue *s)
{
    queue       tmp = NULL, var = NULL;
    void        *el;
    var = *s;
    
    if (var->next == var)
    {
        el = var->elem;
        write(STDERR_FILENO, "ok\n", 3);
        var->next = NULL;
        free(var);
        *s = NULL;
    }
    else
    {
        tmp = var->next;
        el = tmp->elem;
        var->next = tmp->next;
        free(tmp);
    }
    
    return el;
}

